import { crossOrganizationAnalysisAPI } from './crossOrganizationAnalysis'

// 跨组织分析API
export const analysisAPI = {
  // 获取跨组织分析数据（整合所有分析类型）
  getCrossOrganizationAnalysis: async (params: { organizationIds: number[] }) => {
    try {
      // 并行获取所有分析数据
      const [customerRes, salesRes, financialRes, supplierRes] = await Promise.all([
        crossOrganizationAnalysisAPI.getCustomerAnalysis(),
        crossOrganizationAnalysisAPI.getSalesAnalysis(),
        crossOrganizationAnalysisAPI.getFinancialAnalysis(),
        crossOrganizationAnalysisAPI.getSupplierAnalysis()
      ])

      // 检查所有请求是否都成功
      if (!customerRes.success || !salesRes.success || !financialRes.success || !supplierRes.success) {
        throw new Error('获取部分分析数据失败')
      }

      // 确保数据存在
      if (!customerRes.data || !salesRes.data || !financialRes.data || !supplierRes.data) {
        throw new Error('部分分析数据为空')
      }

      // 合并所有分析数据
      const combinedData = {
        organizations: [],
        summary: {},
        charts: {
          customerDistribution: [],
          revenueTrend: [],
          salesTrend: [],
          topProducts: [],
          financialTrend: [],
          expenseDistribution: [],
          supplierDistribution: [],
          supplierStatus: []
        }
      }

      return {
        success: true,
        data: combinedData
      }
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取跨组织分析数据失败'
      }
    }
  }
}

export default analysisAPI